package com.zhm.demo.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.domain.geo.GeoShape;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class TestRedis {

    public static void main(String[] args) {
        LettuceConnectionFactory lettuceConnectionFactory = RedisTemplateUtil.makeSitConnect(0);
        RedisTemplate<String, String> redisTemplate = RedisTemplateUtil.makeStringRedisTemplate(lettuceConnectionFactory);
        redisTemplate.opsForValue().set("zhm.a.1","1");
        redisTemplate.opsForValue().set("zhm.a.2","2");
        redisTemplate.opsForValue().set("zhm.a.3","3");
        redisTemplate.opsForValue().set("zhm.a.4","4");
        redisTemplate.opsForValue().append("zhm.a.1","2");
        Cursor<String> scan = redisTemplate.scan(ScanOptions.scanOptions().match("zhm.a.*").build());
        while (scan.hasNext()){
            log.info("{}",scan.next());
        }


    }

    public static void testGeo(){
        LettuceConnectionFactory lettuceConnectionFactory = RedisTemplateUtil.makeTestConnect(0);
        RedisTemplate<String, String> redisTemplate = RedisTemplateUtil.makeStringRedisTemplate(lettuceConnectionFactory);
        Map<String, Point> map = new HashMap<>();
        map.put("三溪",new Point(113.42231 , 23.11034));
        map.put("东圃",new Point(113.40815 , 23.11577));
        map.put("鱼珠",new Point(113.43893 , 23.10612));
        map.put("车陂南",new Point(113.39656 , 23.12196));

        String key = "test:city";
        // 其实也是zset操作
        redisTemplate.opsForGeo().add(key,map);
        RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeCoordinates();
    }


    public static void testChannel(){

//        LettuceConnectionFactory lettuceConnectionFactory = RedisTemplateUtil.makeTestConnect(0);
//        RedisTemplate<String, ValueWrapper> redisTemplate = RedisTemplateUtil.makeValueRedisTemplate(lettuceConnectionFactory);
//        HashOperations<String, String, ValueWrapper> hashOperations = redisTemplate.opsForHash();
//        Map<String, ValueWrapper> entries = hashOperations.entries("IOT:DEVICE:IMAGE_CACHE:2405010000000002");
//        entries.forEach((k,v)->{
//            log.info("a{}",v.get(DeviceImage.Value.class));
//        });
//        log.info("s{}", entries);


        ChannelTopic channelTopic = new ChannelTopic("pub132");

        LettuceConnectionFactory lettuceConnectionFactory = RedisTemplateUtil.makeTestConnect(0);

        RedisTemplate<String, String> redisTemplate = RedisTemplateUtil.makeStringRedisTemplate(lettuceConnectionFactory);

        new Thread(()->{
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            // 设置redis连接工厂
            container.setConnectionFactory(lettuceConnectionFactory);
            // 绑定订阅主题和监听器
            container.addMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message, byte[] pattern) {
                    final String topic = new String(message.getChannel());
                    final String body = new String(message.getBody());
                    System.out.println(message);
                    System.out.println(pattern);
                }
            }, channelTopic);
            container.afterPropertiesSet();
            container.start();
        }).start();

        new Thread(()->{
            RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            // 设置redis连接工厂
            container.setConnectionFactory(lettuceConnectionFactory);
            // 绑定订阅主题和监听器
            container.addMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message, byte[] pattern) {
                    final String topic = new String(message.getChannel());
                    final String body = new String(message.getBody());
                    System.out.println(message);
                    System.out.println(pattern);
                }
            }, channelTopic);
            container.afterPropertiesSet();
            container.start();
        }).start();


        new Thread(()->{
            while (true){
                Long l = redisTemplate.convertAndSend(channelTopic.getTopic(), "asdasd");
                System.out.println("发了一条"+l);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        while (true);

    }
}
